from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import os

from file_model import FileModel


class ListView(QListView):
    def __init__(self):
        super().__init__()
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested[QPoint].connect(self.pop_menu)
        self.setIconSize(QSize(100, 100))
        self.setWordWrap(True)
        self.setResizeMode(QListView.Adjust)
        self.setViewMode(QListView.IconMode)
        self.fileModel = FileModel()
        self.fileModel.setSorting(QDir.DirsFirst)
        self.setModel(self.fileModel)
        if not os.path.isdir(self.RootDir):
            os.makedirs(self.RootDir)
        self.setRootIndex(self.fileModel.index(self.RootDir))

    def delete(self):
        index = self.currentIndex()
        if index.isValid():
            fn = self.fileModel.filePath(index)
            if os.path.isfile(fn):
                os.remove(fn)
                self.fileModel.refresh(self.rootIndex())

    def relpath(self, fn):
        return os.path.relpath(fn, self.RootDir)
